package com.vic.exchange.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import com.vic.utils.RabbitmqUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * 发送者
 */
public class Producer {
    // 交换机
    private static final String EXCHANGE_NAME = "exchange_topic";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitmqUtils.getChannel();

        // 声明一个 topic 类型的交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

        // 队列01的routing key: "*.rabbit.*"
        // 队列02的routing key: "#.clever"  "java.*.*"

        Map<String, String> map = new HashMap<>();
        map.put("python.rabbit.nothing", "被队列 Q1 接收到");
        map.put("java.python.hello", "被队列 Q2 接收到");
        map.put("quick.rabbit.clever", "被队列 Q1 Q2 接收到");
        map.put("java.rabbit.fox", "被队列 Q1 Q2 接收到");

        map.entrySet().forEach((item) -> {
            try {
                // 发送消息
                channel.basicPublish(EXCHANGE_NAME, item.getKey(), MessageProperties.PERSISTENT_TEXT_PLAIN, item.getValue().getBytes(StandardCharsets.UTF_8));
                System.out.println("发送的消息为：" + item.getKey() + " -> " + item.getValue());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        System.out.println("发送消息完成");

    }
}
